// pages/login/login.js
var app = getApp();
var host = app.globalData.host;

Page({

    //页面的初始数据
    data: {
        currentTab: 0,
        tip:"",
        mobile:"",
        countDown: 0,
        verifyCode:"",
    },

    //生命周期函数--监听页面加载
    onLoad() {
        var userId = wx.getStorageSync('userId');
        if(userId=='') {
            this.getLoginCode();
        } else {
            wx.reLaunch({
              url: '../index/index',
            })
        };
    },

    //页签切换
    switchNav:function(e) {
        //console.log(e)
        var that = this;
        var type = e.currentTarget.dataset.current;
        if(this.data.currentTab == type) {
            return false;
        } else {
            that.setData({currentTab:type});
        }
    },

    //获取登录凭证code
    getLoginCode:function() {
        wx.login({
          success: function(data) {
            console.log(data);
            app.globalData.code = data.code;
          },
        })
    },

    //表单提交
    formSubmit:function(e) {
        //console.log(e);
        var that = this;
        var info = e.detail.value;
        var loginName = info.loginName;
        var loginPassword = info.loginPassword;
        var mobile = info.mobile;
        var verifyCode = info.verifyCode;
        var loginType = that.data.currentTab;
        var code = app.globalData.code;
        //验证表单内容
        var ret = that.checkLogin(loginName,loginPassword,mobile,verifyCode,loginType);
        if (ret) {
            var requestTask = wx.request({
              url: host + '/api/user/login',
              method: 'GET',
              data: {
                  'loginName':loginName,
                  'loginPassword':loginPassword,
                  'mobile':mobile,
                  'verifyCode':verifyCode,
                  'loginType':loginType,
                  'code':code,
              },
              success: function(res) {
                // console.log(res);
                var code = res.data.code;
                var msg = res.data.data;
                if (code == '0000') {
                    wx.setStorageSync('userId', msg.user.id);
                    wx.setStorageSync('nickName', msg.user.nickName);
                  //wx.setStorageSync('swx_session', msg.swx_session);
                    wx.setStorageSync('userMobile', msg.user.mobile);
                  //wx.setStorageSync('openId', msg.openId);
                    wx.setStorageSync('token', msg.token);
                    wx.reLaunch({
                      url: '../index/index',
                    });
                } else {
                    that.setData({tip:msg});
                    wx.showToast({
                      title: '登录失败',
                      icon: 'error',
                      duration: 2000,
                    });
                    return false
                }
              }
            })
        } 
    },

    //检查输入合法性
    checkLogin:function(loginName,loginPassword,mobile,verifyCode,loginType) {
        var that = this;
        if(loginType == 0) {
            if(loginName == "") {
                that.setData({tip:'用户名不能为空！'});
                return false
            };
            if(loginPassword == "") {
                that.setData({tip:'密码不能为空！'});
                return false
            };
        } else {
            if(mobile == "") {
                that.setData({tip:'手机号不能为空！'});
                return false
            };
            var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
            if(!myreg.test(mobile)) {
                that.setData({tip:'手机号不合法！'});
                return false
            };
            if(verifyCode == "") {
                that.setData({tip:'验证码不能为空！'});
                return false
            }
        };
        that.setData({tip:""});
        return true
    },

    //获取手机号
    getMobile:function(e) {
        // console.log(e);
        this.setData({mobile:e.detail.value})
    },
    
    //获取验证码
    getCode:function() {
        var that = this;
        if (that.data.mobile == "") {
            that.setData({tip:'手机号不能为空！'});
            return false
        };
        var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
        if(!myreg.test(that.data.mobile)) {
            that.setData({tip:'手机号不合法！'});
            return false
        };
        that.setData({tip:''});
        //设置倒计时
        var countDown = 5;
        that.setData({ countDown });
        var timer = setInterval(function(){
            console.log("----启动倒计时------");
            if (countDown <= 0) {
                clearInterval(timer);
                that.setData({ countDown: 0 });
            } else {
                countDown--;
                that.setData({ countDown })
            }            
        },1000);
        //请求验证码
        wx.request({
          url: host + '/api/user/getVerifyCode',
          method: 'GET',
          data: {
              'mobile': that.data.mobile,
          },
          success: function(res) {
              console.log(res);
              var code = res.data.code;
              var verifyCode = res.data.data;
              if (code == "0000") {
                that.setData({verifyCode:verifyCode})
              } else {
                clearInterval(timer);
                that.setData({
                    countDown: 0
                });
              }
          }
        })
    },

});

